const ninjaCollection = {
    ninjas: ['Yoshi', 'kuma', 'Hattori'],
    get firstNinja() {
        console.log('Getting firstNinja');
        return this.ninjas[0];
    },
    set firstNinja(value) {
        console.log("Setting firstNinja");
        this.ninjas[0] = value;
    }
};
console.log(ninjaCollection.firstNinja);
ninjaCollection.firstNinja = 'Hachi';
console.log(ninjaCollection.firstNinja);

//es6中
class NinjaCollection {
    constructor() {
        this.ninjas = ["Yoshi", "kuma", "Hattori"];
    }
    get firstNinja() {
        console.log("Getting firstNinja");
        return this.ninjas[0];
    }
    set firstNinja(value) {
        console.log("Setting firsNinja");
        this.ninjas[0] = value;
    }
}
const ninja1 = new NinjaCollection();
console.log(ninja1.firstNinja);
ninja1.firstNinja = "guojing";
console.log(ninja1.firstNinja);

//通过Object.defineProperty
function Ninja() {
    let _skillLevel = 0;
    Object.defineProperty(this, 'skillLevel', {
        get: () => {
            console.log("The get method is called");
            return _skillLevel;
        },
        set: value=> {
            console.log("The set method is called");
            _skillLevel = value;
        }
    })
}
const ninja2 = new Ninja();
console.log(ninja2._skillLevel)
console.log(ninja2.skillLevel)
ninja2.skillLevel = 10;
console.log(ninja2.skillLevel)